home *** CD-ROM | disk | FTP | other *** search
/ Our Solar System / Our Solar System.iso / shuttle / seesat3 / driver.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-24  |  8.1 KB  |  386 lines

  1. /*
  2. DRIVER.C
  3. by Paul S. Hirose, 1990 Nov 12
  4. main module for the SEESAT satellite tracking program.
  5.  
  6. This file is in the public domain.
  7.  
  8. 0 - 99
  9.  
  10. library functions and #defines used in this file:
  11.     atof() exit() fabs() NULL printf() strcmp()
  12. */
  13.  
  14. #include "B:SEESAT.H"    /* global header */
  15.  
  16. extern void exit(), printf();
  17. extern int strcmp();
  18. extern double atof(), fabs();
  19.  
  20. /* used in main(), part of startup message */
  21. #define VERS "1990 Nov 12"
  22.  
  23. /*##################### STATIC FUNCTIONS #####################*/
  24.  
  25. static void
  26.     aop(), b(), bye(), center(), e(), help(), inc(), init(), ma(), mm(),
  27.     offset(), prnval(), raan(), run(), span(), start(), step(), stop();
  28.  
  29. static double elmod();
  30.  
  31. /*############################## DATA ##############################*/
  32.  
  33. static double
  34.     tf,    /* end of prediction run */
  35.     delt,    /* time interval between predictions */
  36.     tspan;    /* length of run */
  37.  
  38. static struct jdtim t1;        /* holds either START or CENTER value */
  39.  
  40. static void (*model)() = sgp4;    /* ptr to prediction model */
  41.  
  42. struct comd {
  43.     char *name;        /* name of command */
  44.     void (*funp)();        /* ptr to function that executes it */
  45. };
  46.  
  47. static struct comd comds[] = {
  48.     {"STEP", step},
  49.     {"START", start},
  50.     {"STOP", stop},
  51.     {"SPAN", span},
  52.     {"CENTER", center},
  53.         /* these commands are in READEL.C */
  54.     {"INDEX", index},
  55.     {"LOAD", load},
  56.     {"OPEN", opn},
  57.         /* ASTRO.C */
  58.     {"PARA", parall},
  59.     {"SUN", dusk},
  60.     {"MOON", moon},
  61. #if ENPRE
  62.     {"PRECESS", setep},
  63. #endif
  64.         /* in this file */
  65.     {"AOP", aop},
  66.     {"B", b},
  67.     {"E", e},
  68.     {"I", inc},
  69.     {"MA", ma},
  70.     {"MM", mm},
  71.     {"OFFSET", offset},
  72.     {"RAAN", raan},
  73.  
  74.     {"HELP", help},
  75.     {"EXIT", bye},
  76.     {NULL}
  77. };
  78.  
  79. static int cspan;        /* flag; 1 = CENTER/SPAN mode */
  80.  
  81. static char *nullp = NULL;    /* constant */
  82.  
  83. /*############################## CODE ##############################*/
  84.  
  85. main()
  86. {
  87.     char *cp1, *cp2;
  88.     struct comd *comdp;
  89.  
  90.     ETEST((0, 1, 3, &pi));    /* dummy call to get control of brkpt */
  91.  
  92.     printf("SEESAT %s by Paul S. Hirose\n\n", VERS);
  93.  
  94.     init();            /* initialize constants */
  95.     zd = topos();    /* get observer's coordinates, set time zone */
  96.     printf("\ntype HELP to see commands\n");
  97.  
  98.     /* Execution is vectored back here for all errors */
  99.     SETJMP(reset);
  100.  
  101.     tokp = &nullp;    /* clear command line */
  102.  
  103.     for (;;) {    /* main loop */
  104.  
  105.         while (*tokp == NULL)
  106.         /* command line is empty */
  107.             tok();        /* input new cmd line */
  108.  
  109.         cp1 = *tokp;        /* point to current command */
  110.  
  111.         /* Find the command in the comds[] table, execute the
  112.         corresponding function */
  113.  
  114.         for (comdp = comds; (cp2 = comdp->name) && strcmp(cp1, cp2);
  115.           ++comdp)
  116.             ;
  117.         if (cp2)    /* found command; execute */
  118.             (*comdp->funp)();
  119.         else {
  120.             printf("%s: INVALID COMMAND\n", cp1);
  121.             LONGJMP(reset, 1);
  122. }    }    }
  123.  
  124.  
  125. static void
  126. aop()        /* alter argument of the perigee */
  127. {
  128.     omegao = elmod() * de2ra;
  129. }
  130.  
  131.  
  132. static void
  133. b()        /* alter bstar */
  134. {
  135.     bstar = elmod();
  136. }
  137.  
  138.  
  139. static void
  140. bye()
  141. {
  142.     hfree();    /* release index space */
  143.     exit(0);
  144. }
  145.  
  146.  
  147. static void
  148. center()
  149. {
  150.     tokjum(&t1);        /* load t1 with CENTER value */
  151.     cspan = 1;        /* center/span mode */
  152.     run();
  153. }
  154.  
  155.  
  156. static void
  157. e()        /* alter eccentricity */
  158. {
  159.     eo = elmod();
  160. }
  161.  
  162.  
  163. static double
  164. elmod()
  165. /* tasks common to nearly all commands that modify orbital elements */
  166. {
  167.     tokp += 2;    /* point to next command */
  168.     iflag = 1;    /* since elements will be altered */
  169.     return atof(*(tokp - 1));
  170. }
  171.  
  172.  
  173. static void
  174. help()
  175. {
  176.     ++tokp;        /* advance to next command */
  177.     printf("\nOFFSET/SPAN/STEP <time>\n");
  178.     printf("   <time> = hhmm:ss.s...\n");
  179.     printf("   leading zeros   : and seconds   . and decimal seconds\n");
  180.     printf("   are optional.  Minus sign allowed on OFFSET argument\n\n");
  181.  
  182. #if ENPRE
  183.     printf("CENTER/MOON/PARA/PRECESS/START/STOP/SUN <date> <time>\n");
  184. #else
  185.     printf("CENTER/MOON/PARA/START/STOP/SUN <date> <time>\n");
  186. #endif
  187.     printf("   e.g., START 1990 JUN 9 2100\n");
  188.     printf("   Omit y, y/m, or y/m/d for default to last value\n\n");
  189.  
  190.     printf("OPEN <file name>\n");
  191.     printf("LOAD <satellite name>\n\n");
  192.  
  193.     printf("MM <revs per day>\n");
  194.     printf("E <eccentricity>\n");
  195.     printf("AOP/I/MA/RAAN <degrees>\n");
  196.     printf("B <bstar>\n\n");
  197.  
  198.     printf("EXIT/HELP/INDEX take no arguments\n\n");
  199. }
  200.  
  201.  
  202. static void
  203. inc()        /* alter inclination */
  204. {
  205.     xincl = elmod() * de2ra;
  206. }
  207.  
  208.  
  209. static void
  210. init()
  211. /* Initialize 3rd & 4th gravitational zonal harmonics.  Must be done at run
  212. time because my compiler (Ecosoft 3.50 for CP/M) won't allow negative
  213. initializers! */
  214. {
  215.     xj3 = -.253881e-5;
  216.     xj4 = -1.65597e-6;
  217. }
  218.  
  219.  
  220. static void
  221. ma()        /* alter mean anomaly */
  222. {
  223.     xmo = elmod() * de2ra;
  224. }
  225.  
  226.  
  227. static void
  228. mm()        /* alter mean motion */
  229. {
  230.     xno = elmod() * twopi / xmnpda;        /* radians/min */
  231. }
  232.  
  233.  
  234. static void
  235. offset()
  236. /* Apply a time offset to epoch of elements */
  237. {
  238.     toffs = atomin(*++tokp);
  239.     ++tokp;        /* point to next cmd */
  240.     /* Do not set iflag since initialization is independent of time */
  241. }
  242.  
  243.  
  244. static void
  245. prnval(time)
  246. double time;    /* local time */
  247. /* All tabular data from a prediction run is printed here.  Except for local
  248. time of day, all data comes from global variables in SEESAT.H. */
  249. {
  250.     char **cpp;
  251.  
  252.     /* time, azimuth, elevation. */
  253.     printf("%s %3d%3d  ", timstr(time),
  254.       (int) (azel.x * ra2de + .5), (int) (azel.y * ra2de + .5));
  255.  
  256.     /* Right Ascension, declination. */
  257.     cpp = degdms(2, radec.x / twopi * 24.);
  258.     printf("%2sh%2sm ", cpp[0], cpp[1]);
  259.     cpp = degdms(1, radec.y * ra2de);
  260.     printf("%3s %2s'  ", cpp[0], cpp[1]);
  261.  
  262.     /* slant range, sun elevation at satellite */
  263.     printf("%5ld %3d  ", (long int) (radec.z * xkmper), elsusa);
  264.  
  265.     /* altitude */
  266.     printf("%5ld ", (long int) (latlon.z * xkmper));
  267.  
  268.     /* latitude */
  269.     printf("%2d%c ", (int) (fabs(latlon.y) * ra2de + .5),
  270.       (latlon.y >= 0.) ? 'N' : 'S');
  271.  
  272.     /* longitude */
  273.     printf("%3d%c\n", (int) (fabs(latlon.x) * ra2de + .5),
  274.       (latlon.x >= 0.) ? 'E' : 'W');
  275. }
  276.  
  277.  
  278. static void
  279. raan()        /* alter Right Ascension of Ascending Node */
  280. {
  281.     xnodeo = elmod() * de2ra;
  282. }
  283.  
  284.  
  285. static void
  286. run()
  287. /* Execute prediction run.  A call of this function is the last statement in
  288. start(), stop(), step(), span(), and center().  If there's nothing else in
  289. the command line to process, then automatically begin a prediction run. 
  290. Otherwise, return without taking any action. */
  291. {
  292.     double tsince, mins, temp;
  293.     long int jd;
  294.     unsigned int count;
  295.  
  296.     if (*tokp)    /* haven't reached end of cmd line yet */
  297.         return;
  298.  
  299.     if (delt <= 0.) {
  300.         printf("STEP MUST BE > 0\n");
  301.         LONGJMP(reset, 1);
  302.     }
  303.     jd = t1.jd;
  304.     mins = t1.time;
  305.     /* jd & mins are local time */
  306.  
  307.     tsince = t1.jd * xmnpda - 720. + t1.time + zd - epoch;
  308.  
  309.     if (cspan) {
  310.     /* tsince = (center of run) - (elements epoch) */
  311.         temp = .5 * tspan;
  312.         mins -= temp;
  313.         while (mins < 0.) {    /* start on previous day */
  314.             mins += xmnpda;
  315.             --jd;
  316.         } tsince -= temp;
  317.     } else {
  318.     /* tsince = (start of run) - (elements epoch) */
  319.         if (tf - epoch < tsince) {
  320.             printf("START TIME IS AFTER STOP TIME\n");
  321.             LONGJMP(reset, 1);
  322.         } tspan = tf - (epoch + tsince);
  323.     }
  324.     count = tspan / delt + 1.95;
  325.     printf("%s   ", name);    /* satellite name */
  326.     printf("%s", jdstr(jd));    /* print date */
  327.     if (toffs)
  328.         printf("   offset = %s", timstr(toffs));
  329.     printf("\n");
  330.     printf("  time   az el   R.A.    dec    range sun    alt l\
  331. at  lon\n");
  332.  
  333.     do {
  334.         if (mins >= xmnpda) {        /* crossed midnight */
  335.             mins -= xmnpda;
  336.             printf("%s\n", jdstr(++jd));    /* print new date */
  337.         }
  338.         FTEST((1, 1, 2, &tsince));
  339.         (*model)(tsince - toffs);    /* call prediction model */
  340.         DTEST((2, 1, &elsusa));
  341.         if (xyztop(tsince + epoch)) {
  342.         /* above horizon; print line of data */
  343.             FTEST((3, 1, 2, &mins));
  344.             prnval(mins);
  345.         } mins += delt;
  346.         tsince += delt;
  347.     } while (--count);
  348. }
  349.  
  350.  
  351. static void
  352. span()
  353. {
  354.     tspan = atomin(*++tokp);
  355.     ++tokp;            /* point to next command */
  356.     cspan = 1;        /* center/span mode */
  357.     run();
  358. }
  359.  
  360.  
  361. static void
  362. start()
  363. {
  364.     tokjum(&t1);        /* load struct with START time */
  365.     cspan = 0;        /* start/stop mode */
  366.     run();
  367. }
  368.  
  369.  
  370. static void
  371. step()
  372. {
  373.     delt = atomin(*++tokp);
  374.     ++tokp;        /* point to next command */
  375.     run();
  376. }
  377.  
  378.  
  379. static void
  380. stop()
  381. {
  382.     tf = tokmin();
  383.     cspan = 0;        /* start/stop mode */
  384.     run();
  385. }
  386. t =